/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package marsaMaroc.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import marsaMaroc.model.Client;
import marsaMaroc.model.Facture;
import marsaMaroc.model.Reclamation;

/**
 *
 * @author Administrateur
 */
public class ClientDAO extends DAO<Client> {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("Marsa-MarocPU");
    EntityManager em;

    @Override
    public void add(Client object) {
        em = emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(object);
        em.getTransaction().commit();
    }

    @Override
    public void delete(Client object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void update(Client object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Client get(int id) {
         em = emf.createEntityManager();
        return em.find(Client.class, id);
    }

    public Client getByAccount(int id) {
        em = emf.createEntityManager();
        Query q = em.createQuery("select e FROM Client e where e.compte.id =" + id);
        return (Client) q.getSingleResult();
    }

    @Override
    public List<Client> getAll() {
        em = emf.createEntityManager();
        Query q = em.createQuery("select e FROM Client e");
        return q.getResultList();
    }

    public List<Facture> getAllFactures(Client c) {
        em = emf.createEntityManager();
        Query q = em.createQuery("select f FROM Facture f where f.client.codeClient =" + c.getCodeClient());
        return  q.getResultList();
    }

    public List<Reclamation> getAllReclamations(Client c) {
        em = emf.createEntityManager();
        Query q = em.createQuery("select f FROM Reclamation f where f.facture.client.codeClient="+ c.getCodeClient());
        return q.getResultList();
    }

}
